package com.esms.controller;

import com.esms.dao.EmployeeCustomVoMapper;
import com.esms.po.Employee;
import com.esms.service.EmployeeService;
import com.esms.utils.AESUtil;
import com.esms.utils.MD5Utils;
import com.esms.utils.Message;
import com.esms.vo.EmployeeCustomVo;
import com.esms.vo.EmployeePages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 评价员工Controller
 * @Author: ko
 * @Date: 2022/1/8 15:19
 */
@Controller
@RequestMapping("/pjEmployee")
public class PjEmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeCustomVoMapper employeeCustomVoMapper = null;


    @RequestMapping("/findSelective.do")
    @ResponseBody
    public EmployeePages findSelective(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value="e_account", defaultValue="") String e_account,
            @RequestParam(value="e_name", defaultValue="") String e_name,
            @RequestParam(value="d_id", defaultValue="0") int d_id) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("e_account",e_account);
        map.put("e_name",e_name);
        map.put("d_id",d_id);
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        List<EmployeeCustomVo> list =
                employeeCustomVoMapper.selectEmployeeSelective(map);
        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        EmployeePages employeePages = new EmployeePages();
        employeePages.setCode(0);
        employeePages.setMsg("");
        employeePages.setCount((int) pageResult.getTotal());
        employeePages.setData(pageResult.getList());
        return employeePages;
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @RequestMapping("/selectVoByPrimaryKey.do")
    @ResponseBody
    public EmployeeCustomVo selectVoByPrimaryKey(int id) {
        EmployeeCustomVo monthlyAttendanceCustomVo = new EmployeeCustomVo();
        monthlyAttendanceCustomVo = employeeCustomVoMapper.selectVoByPrimaryKey(id);
        return monthlyAttendanceCustomVo;
    }


    /**
     * 添加员工信息操作
     * @throws Exception
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Message add(Employee employee) throws Exception {
    	if(StringUtils.isEmpty(employee.geteName())) {
    		return Message.error("请填写员工姓名");
    	}
    	if(StringUtils.isEmpty(employee.geteIdcard())) {
    		return Message.error("请填写身份证号码");
    	}
//    	if(StringUtils.isEmpty(employee.geteEmail())) {
//    		return Message.error("请填写邮箱");
//    	}
    	if(StringUtils.isEmpty(employee.getePhone())) {
    		return Message.error("请填写手机号");
    	}
    	if(StringUtils.isEmpty(employee.geteBirthday())) {
    		return Message.error("请填写生日");
    	}
    	
    	if(StringUtils.isEmpty(employee.geteBasePay())) {
    		return Message.error("请填写基本工资");
    	}
        //检测工号是否相同
        //查找是否同名
        Employee e = employeeService.findByeAccount(employee.geteAccount());
        if(e!=null) {
        	return Message.error("工号已存在");
        }
//        	employee.setePassword(MD5Utils.encodeByMD5(employee.getePassword()));
//            String KEY = "1234567887654321";
            employee.setePassword(AESUtil.aesEncrypt(employee.getePassword(), AESUtil.getKEY()));
            employee.seteIsdel(1);
            employeeService.insert(employee);
            return Message.success("添加员工成功");

    }



    /**
     * 编辑员工信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/editEmployee.do")
    @ResponseBody
    public Message editEmployee(Employee employee) throws Exception {
    	
    	if(StringUtils.isEmpty(employee.geteName())) {
    		return Message.error("请填写员工姓名");
    	}
    	if(StringUtils.isEmpty(employee.geteIdcard())) {
    		return Message.error("请填写身份证号码");
    	}
//    	if(StringUtils.isEmpty(employee.geteEmail())) {
//    		return Message.error("请填写邮箱");
//    	}
    	if(StringUtils.isEmpty(employee.getePhone())) {
    		return Message.error("请填写手机号");
    	}
    	if(StringUtils.isEmpty(employee.geteBirthday())) {
    		return Message.error("请填写生日");
    	}
    	
    	if(StringUtils.isEmpty(employee.geteBasePay())) {
    		return Message.error("请填写基本工资");
    	}
        employeeService.updateByPrimaryKeySelective(employee);
        return Message.success("员工信息编辑成功");
    }

    /**
     * 删除
     * @param id
     * @throws Exception
     */
    @RequestMapping("/deleteByPrimaryKey.do")
    @ResponseBody
    public Message deleteByPrimaryKey(int id) throws Exception {
        //假删除，设置离职时间
        Employee employee = new Employee();
        employee.seteId(id);
        employee.seteLeaveTime(new Date());
        employee.seteIsdel(0);
        //employeeService.updateByPrimaryKeySelective(employee);
        return Message.success();
    }

    /**
     * 删除
     * @param id
     * @throws Exception
     */
    @RequestMapping("/EmpEvaDataSubmit.do")
    @ResponseBody
    public Message EmpEvaDataSubmit(int id,String ids1) throws Exception {
        //假删除，设置离职时间
    	
    	System.out.println("data1====="+ids1);
    	
        Employee employee = new Employee();
        employee.seteId(id);
        employee.seteLeaveTime(new Date());
        employee.seteIsdel(0);
        //employeeService.updateByPrimaryKeySelective(employee);
        return Message.success();
    }
    
    /**
     * 批量删除
     * @param ids
     */
    @RequestMapping("/deleteByQuery.do")
    @ResponseBody
    public Message deleteByQuery (@RequestParam(value = "arr")int[] ids,@RequestParam(value = "arr1")int[] ids1) {
    
    	System.out.println("ids1=="+ids1);
    	
        //假删除
        //如果有id才执行
        if(ids.length > 0) {
            //employeeService.deleteByQuery(ids);
        }
        return Message.success("删除成功");

    }



}
